import { Context, EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
import * as path from 'path';
import * as consulConfig from '../service.json';

export default (appInfo: EggAppInfo) => {
  // tslint:disable:no-object-literal-type-assertion
  const config = {} as PowerPartial<EggAppConfig>;

  // override config from framework / plugin
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1573032782275_6182';

  // add your egg config in here
  config.middleware = ['notfoundHandler'];

  // 关闭默认的overrideMethod中间件
  config.overrideMethod = {
    enable: false,
  };

  // security
  config.security = {
    csrf: false,
  };

  config.proxy = true;

  // bodyParser
  config.bodyParser = {
    formLimit: '50mb',
    jsonLimit: '50mb',
  };

  // onerror
  config.onerror = {
    all(err: Error, ctx: Context) {
      ctx.logger.error('Caught exception:----->', err);
      // 在此处定义针对所有响应类型的错误处理方法
      // 注意，定义了 config.all 之后，其他错误处理方法不会再生效
      ctx.set('content-type', 'application/json');
      ctx.status = 500;
      ctx.body = JSON.stringify({
        success: false,
        message: '500 Internal Server Error',
      }, null, 2);
      ctx.logger.info(`response [${ctx.tracer.traceId}] to client: ${ctx.body}`);
    },
  };

  // consul
  (config.m7Consul as any) = consulConfig;

  const logDir = (consulConfig.service.version_tag.indexOf('v-dev') !== -1 || consulConfig.service.version_tag.indexOf('v-release') !== -1) ?
    `/data/logs/${consulConfig.service.version_tag}-${consulConfig.service.name}-${consulConfig.service.port}` :
    path.join(appInfo.root, 'logs');
  // logger
  config.logger = {
    appLogName: path.join(logDir, 'app_log.log'),
    coreLogName: path.join(logDir, 'app_core_log.log'),
    agentLogName: path.join(logDir, 'app_log.log'),
    errorLogName: path.join(logDir, 'error_log.log'),
  };

  // customLogger
  config.customLogger = {
    scheduleLogger: {
      consoleLevel: 'NONE',
      file: path.join(logDir, 'app_schedule.log'),
    },
  };

  // logrotator 日志切割
  config.logrotator = {
    filesRotateBySize: [
      path.join(logDir, 'app_log.log'),
      path.join(logDir, 'app_core_log.log'),
      path.join(logDir, 'error_log.log'),
      path.join(logDir, 'app_schedule.log'),
    ],
    maxFileSize: 20 * 1024 * 1024,
    maxFiles: 20,
  };

  // m7Http
  config.m7Http = {
    defaultTimeout: 30000,
  };

  // clog
  config.clog = {
    enable: false,
    redisHost: '127.0.0.1',
    redisPort: 6379,
    redisPass: '',
    redisDB: 1,
    listName: 'clog',
    baseLogFields: {
      source: 'cc-egg-timer',
      hostname: '',
    },
    loggerOptions: {
      level: 'INFO',
      eol: '',
    },
  };

  // ipAuth
  config.ipAuth = {
    dbCluster: 'commonDB',
    dbDB: 'cc',
    dbCollection: 'access_ip',
  };

  // opentracing
  config.opentracing = {
    collector: {
      log: false,
    },
  };

  // the return config will combines to EggAppConfig
  return {
    ...config,
  };
};
